
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:xim/common/style/colors.dart';
import 'package:xim/config/route_config.dart';
import 'package:xim/http/apis/user/user_api.dart';
import 'package:xim/http/apis/user/user_model.dart';
import 'package:xim/widget/common/state.dart';

class AddFriendSearchPage extends StatefulWidget {

  const AddFriendSearchPage({Key? key}) : super(key: key);

  @override
  State<AddFriendSearchPage> createState() => _AddFriendSearchPageState();
}

class _AddFriendSearchPageState extends State<AddFriendSearchPage> with BrightnessMode{

  final ModelColor textColor = ModelColor(lightColor: const Color((0xff161616)), darkColor: const Color(0xffcecece));
  final ModelColor bgColor = ModelColor(darkColor: const Color(0xff111111), lightColor: const Color(0xffededed));
  final ModelColor inputFgColor = ModelColor(darkColor: const Color(0xff191919), lightColor: const Color(0xfffefefe));
  final ModelColor weakTextColor = ModelColor(lightColor: const Color(0xffb2b2b2), darkColor: const Color(0xff5b5b5b));
  final ModelColor cancelTextColor = ModelColor(lightColor: const Color(0xff586b95), darkColor: const Color(0xff586b95));

  final inputFocusNode = FocusNode();

  @override
  List<ModelColor> get brightnessColors => [bgColor,textColor,inputFgColor,weakTextColor,cancelTextColor];

  List<UserInfoModel>? users;

  @override
  Widget build(BuildContext context) {
    initModelColor(context);
    return Container(
      color: bgColor.color,
      width: double.infinity,
      height: double.infinity,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SafeArea(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Row(
                children: [
                  Expanded(
                    child: Container(
                      height: 34,
                      alignment: const Alignment(0, 0),
                      decoration: BoxDecoration(
                          color: inputFgColor.color,
                          borderRadius: const BorderRadius.all(Radius.circular(10))
                      ),
                      child: TextField(
                        autofocus: true,
                        onChanged: (e){
                          if(users!=null){
                            setState(() {
                              users = null;
                            });
                          }
                        },
                        onSubmitted: (e) async {
                          var searchResult = await UserApi.searchByUname(e);
                          setState(() {
                            users = searchResult;
                          });
                          if(searchResult.isNotEmpty){
                            var user = searchResult[0];
                            Routers.toProfilePublicPage(user.account);
                          }
                        },
                        focusNode: inputFocusNode,
                        style: TextStyle(color: textColor.color, fontSize: 14),
                        decoration: InputDecoration(
                          prefixIcon: Icon(Icons.search, size: 18, color: textColor.color,),
                          hintText: "账号",
                          hintStyle: TextStyle(fontSize: 14, color: weakTextColor.color),
                          enabledBorder: InputBorder.none,
                          border: InputBorder.none,
                          disabledBorder: InputBorder.none,
                          focusedBorder: InputBorder.none,
                        ),
                      ),
                    ),
                  ),
                  const SizedBox(width: 12,),
                  GestureDetector(
                    onTap: (){
                      FocusScope.of(context).requestFocus(FocusNode());
                      Routers.back();
                    },
                    child: Text("取消", style: TextStyle(color: cancelTextColor.color),)
                  )
                ],
              ),
            ),
          ),
          const SizedBox(height: 12,),
          if(users!=null && users!.isEmpty)
            Center(
              child: Text("该用户不存在", style: TextStyle(color: textColor.color),),
            ),
        ],
      ),
    );
  }
}
